/*
 * Copyright Radosław Smogura, 2012. All rights reserved.
 *
 * The content of this file is confidential and it is intelectual property
 * of copyright owners.
 * 
 *
 * $URL: http://reprap-ide.googlecode.com/svn/trunk/RepRapIDE/RepRapProject/src/main/java/eu/softper/reprap/project/nodes/SourcesDirectoryNode.java $
 * $Id: SourcesDirectoryNode.java 3 2012-11-19 10:58:30Z mail@smogura.eu $
 * $Revision: 3 $
 */
package eu.softper.reprap.project.nodes;

import eu.softper.reprap.project.RepRapProject;
import eu.softper.reprap.project.nodes.actions.AddStlFileAction;
import javax.swing.Action;
import org.openide.filesystems.FileObject;
import org.openide.loaders.DataFilter;
import org.openide.loaders.DataObject;
import org.openide.nodes.FilterNode;
import org.openide.nodes.Node;
import org.openide.util.Lookup;
import org.openide.util.lookup.Lookups;
import org.openide.util.lookup.ProxyLookup;

/**
 *
 * @author rsmogura
 */
public class SourcesDirectoryNode extends FilterNode {
    
    protected SourcesDirectoryNode(Node original, Childern children, Lookup lookup) {
        super(original, children, lookup);
    }
    
    @Override
    public Action[] getActions(boolean context) {
        Action[] superActions = super.getActions(context);
        Action[] result = new Action[superActions.length + 1];
        int pos = superActions.length;
        System.arraycopy(superActions, 0, result, 0, superActions.length);
        
        result[pos++] = AddStlFileAction.getInstance();
        
        return result;
    }

    public static SourcesDirectoryNode createFolderNode(RepRapProject project, Node delegated) {
        ProxyLookup lookups = new ProxyLookup(Lookups.fixed(project), delegated.getLookup());
        Childern filterChildren = new Childern(project, delegated);
        
        return new SourcesDirectoryNode(delegated, filterChildren, lookups);
    }
    
    public static Node createFileNode(RepRapProject project, Node delegated) {
        ProxyLookup lookups = new ProxyLookup(Lookups.fixed(project), delegated.getLookup());
        
        return new FilterNode(delegated, org.openide.nodes.Children.LEAF, lookups);
    }
    
    private static class Childern extends FilterNode.Children {
        private RepRapProject project;
        
        public Childern(RepRapProject project, Node original) {
            super(original);
            this.project = project;
        }

        @Override
        protected Node[] createNodes(Node key) {
            DataObject dataObject = key.getLookup().lookup(DataObject.class);
            if (dataObject != null && dataObject.isValid()) {
                FileObject fo = dataObject.getPrimaryFile();
                if (fo.isFolder())
                    return new Node[] { createFolderNode(project, key) };
                else
                    return new Node[] { createFileNode(project, key) };
            }else {
                return new Node[] { key };
            }
        }
    }
    
    protected static class ProjectSourcesFilter implements DataFilter {
        private static ProjectSourcesFilter instance = new ProjectSourcesFilter();

        public static ProjectSourcesFilter getInstance() {
            return instance;
        }
        
        @Override
        public boolean acceptDataObject(DataObject obj) {
            if (!obj.isValid())
                return false;
            FileObject fo = obj.getPrimaryFile();
            return (fo.isFolder() || (fo.isData() && fo.getExt().toLowerCase().equals("stl"))) && fo.isValid() && !fo.isVirtual();
        }
        
    }
}
